<?php
require_once('lanceur_spip.php');
include_spip('functions/pdk_common');
//require_once(_DIR_PLUGIN_ALOHACART.'classes/ClientModel.php');
require_once(_DIR_PLUGIN_PDK.'classes/simpletest/class.PdkTest.php');

/**
 *  ExtensionModel class test case
 */
class TestClassAlohacartExtension extends PdkTest {
    private $extension;
    
    /**
     *  AlohacartExtensionModel
     */
    private $extension_mdl;
	
	/**
     *  Alohacart
     */
    private $cart;
	
	/**
	 * EcataloguE Currency model
	 */
	private $currency_mdl;
	
	/**
	 * EcataloguE Product model
	 */
	private $product_mdl;
    
    function __construct(){
        parent::__construct();
        // Define all additional models:
        $pdk_models = array();
        $this->extension_mdl = pdk_load_class('AlohacartExtensionModel','classes');
        $pdk_models['AlohacartExtensionModel'] = $this->extension_mdl;        
		$this->currency_mdl = pdk_load_class('CurrencyModel','classes');
		$pdk_models['CurrencyModel'] = $this->currency_mdl;        
		$this->product_mdl = pdk_load_class('ProductModel','classes');
		$pdk_models['ProductModel'] = $this->product_mdl;        
        
        $this->AddPdkModels($pdk_models);
		
		$this->cart = pdk_load_class('Alohacart','classes');
        
        // We will test on shipping flat extension    
        $this->extension = pdk_load_class('AlohacartShippingFlat', 'classes/shipping');
                        
        $this->dropTestTables = TESTS_DROP_TEST_TABLES;
        
        // Remember what is written in config
        $this->config = lire_config('alohacart/shipping_flat');
		// Remember what is written in session, so later we can return it to initial state
        @session_start();
        $this->session_variable = &$_SESSION["alohacart"];
        $this->session_data = $this->session_variable; 
    }
        
    /**
     *  Run before each test
     */
    function setUp() {
        parent::setUp();
		
		// Clear cart before each test        
        $this->session_variable = array();
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
        parent::tearDown();		
    }
    
    function __destruct() {
        parent::__destruct();
        // Rewrite config
        ecrire_config('alohacart/shipping_flat', $this->config);
		// Return session variable to initial state after testing                
        $this->session_variable = $this->session_data;
    }
    
    function Test_SaveMetaData() {
        $data_check = array('field1'=>'Test','field2'=>'Test2');
        $this->extension->SaveMetaData($data_check);
        $data = lire_config('alohacart/shipping_flat');
        $this->assertEqual($data_check, $data);
        
        // If $data is empty, meta data must be deleted
        $this->extension->SaveMetaData();
        $data = lire_config('alohacart/shipping_flat');
        $this->assertNull($data);
    }
    
    function Test_GetMetaData() {
        $data_check = array('field1'=>'Test','field2'=>'Test2');
        ecrire_config('alohacart/shipping_flat', $data_check);
        $data = $this->extension->GetMetaData();
        $this->assertEqual($data_check, $data);
    }
    
	function Test_GetCostFreeFrom() {		
		$this->currency_mdl->SetCurrentCurrencyId(2);
		$this->currency_mdl->main_currency_id = 2;
        $this->extension->SaveMetaData(array('cost'=>'200','free_from'=>'1000','id_currency'=>2));
		
		// 90*10=900 < 1000, so shipping must cost 200
		$this->cart->AddProduct(1, 10);
		$this->assertEqual(200, $this->extension->GetCost());
		
		// 900 + 80*2=1060 > 1000, so shipping must be free
		$this->cart->AddProduct(2, 2);
		$this->assertEqual(0, $this->extension->GetCost());
	}
}